package com.wzy.oa_sys.service.impl.schedule;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzy.oa_sys.mapper.schedule.BoardroomMapper;
import com.wzy.oa_sys.mapper.schedule.UseBoardroomMapper;
import com.wzy.oa_sys.model.pojo.schedule.OaBoardroom;
import com.wzy.oa_sys.model.pojo.schedule.OaUseBoardroom;
import com.wzy.oa_sys.model.request.schedule.BoardroomReq;
import com.wzy.oa_sys.model.request.schedule.UseBoardroomReq;
import com.wzy.oa_sys.model.response.schedule.BoardroomResp;
import com.wzy.oa_sys.service.schedule.BoardroomService;
import com.wzy.oa_sys.service.schedule.UseBoardroomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class BoardroomServiceImpl extends ServiceImpl<BoardroomMapper, OaBoardroom> implements BoardroomService {

    @Autowired
    private BoardroomMapper boardroomMapper;
    @Autowired
    private UseBoardroomMapper useBoardroomMapper;
    @Override
    public List<BoardroomResp> getBoardroomList() {
        List<OaBoardroom> boardroomList = boardroomMapper.selectList(null);
        List<BoardroomResp> boardroomRespList = new ArrayList<>();
        boardroomList.forEach(boardroom -> {
            boardroomRespList.add(BoardroomResp.builder()
                            .id(boardroom.getNBoardroomId())
                    .cno(boardroom.getCNo())
                    .number(boardroom.getNNumber())
                    .notes(boardroom.getCNotes())
                    .build());
        });
        return boardroomRespList;
    }

    @Override
    public void addBoardroom(BoardroomReq boardroomReq) {
        OaBoardroom boardroom = new OaBoardroom();
        boardroom.setCNo(boardroomReq.getCno());
        boardroom.setNNumber(boardroomReq.getNumber());
        boardroom.setCNotes(boardroomReq.getNotes());
        boardroomMapper.insert(boardroom);
    }

    @Override
    public int deleteBoardroomById(Integer id) {
        return boardroomMapper.deleteBoardroomById(id);
    }

    @Override
    public void updateBoardroom(BoardroomReq boardroomReq) {
        QueryWrapper<OaBoardroom> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("n_boardroom_id", boardroomReq.getId());
        OaBoardroom oaBoardroom = boardroomMapper.selectOne(queryWrapper);
        if(oaBoardroom != null){
            if(!oaBoardroom.getCNo().equals(boardroomReq.getCno())){
                oaBoardroom.setCNo(boardroomReq.getCno());
            }
            if(oaBoardroom.getNNumber()!=((int)boardroomReq.getNumber())){
                oaBoardroom.setNNumber(boardroomReq.getNumber());
            }
            if(!oaBoardroom.getCNotes().equals(boardroomReq.getNotes())){
                oaBoardroom.setCNotes(boardroomReq.getNotes());
            }
        }
        boardroomMapper.update(oaBoardroom,queryWrapper);
    }

    @Override
    public List<BoardroomResp> getICanUseBoardroomList(UseBoardroomReq useBoardroomReq) {
        // 查看该时间内有使用的会议室
        List<OaUseBoardroom> overlappingBookings = useBoardroomMapper.findOverlappingBookings1(useBoardroomReq);
        System.out.println("=============="+overlappingBookings);
        // Step 2: 已使用会议室去重
        Set<Long> bookedBoardroomIds = overlappingBookings.stream()
                .map(OaUseBoardroom::getNBoardroomId) // Assuming getBoardroomId() returns the ID of the boardroom
                .collect(Collectors.toSet());

        System.out.println("=========已使用=========="+bookedBoardroomIds);

        // Step 3: 检索所有会议室
        List<OaBoardroom> oaBoardrooms = boardroomMapper.selectList(null);

        // Step 4: 过滤已使用的会议室
        List<OaBoardroom> availableBoardrooms = oaBoardrooms.stream()
                .filter(boardroom -> !bookedBoardroomIds.contains(boardroom.getNBoardroomId())) // Assuming getId() returns the boardroom ID
                .collect(Collectors.toList());

        // Step 5: Convert availableBoardrooms to BoardroomResp list and return
        return availableBoardrooms.stream()
                .map(boardroom -> BoardroomResp.builder()
                        .id(boardroom.getNBoardroomId())
                                .cno(boardroom.getCNo())
                                .number(boardroom.getNNumber())
                                .notes(boardroom.getCNotes()).build()) // Modify according to actual fields
                .collect(Collectors.toList());
    }
}
